# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=graphviz
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=8.1.0
pkgrel=3
pkgdesc="Graph Visualization Software (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://www.graphviz.org/'
license=('spdx:EPL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-devil"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libgd"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-gts"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libltdl")
makedepends=(#"${MINGW_PACKAGE_PREFIX}-ocaml" not tested yet need to build ocaml first
             #"${MINGW_PACKAGE_PREFIX}-lua" problem with distinguishing between the two versions we have and links to the wrong version sigh
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-python"
             #"${MINGW_PACKAGE_PREFIX}-ruby" unable to find ruby's pkgconfig file looks for ruby.pc no ruby$(VERSION).pc as it is named now 
             "${MINGW_PACKAGE_PREFIX}-swig"
             #"${MINGW_PACKAGE_PREFIX}-tcl" needs a path to tclsh.sh
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "groff")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-base: gvedit"
            "${MINGW_PACKAGE_PREFIX}-python: for python modules")
install=${_realname}-${MSYSTEM}.install
source=(https://gitlab.com/graphviz/graphviz/-/archive/${pkgver}/graphviz-${pkgver}.tar.gz
        0001-configure-check-freeglut.patch
        0002-gvedit-fix-relative-qmake-dir.patch
        0003-gdiplus-fix-library.patch
        0004-gvpr-remove-buiddir.patch
        0005-makefile-no-undefined.patch
        0006-tclpkg-fix-dll-extension.patch
        0007-graphviz-workaround-ghostscript-check.patch)
noextract=("graphviz-${pkgver}.tar.gz")
sha256sums=('2e4dfee3c24925ad51d1e76a9fb2b19b26a5a0049ef7be6d3e52667aac72eabe'
            '6ae607fcb534a0950e0140645422244592c746d87215e5152747b4225ceb5b34'
            '1fce2b569602ef02c4b58d032199706b9f65277211f32e834202ba0a7ee312d1'
            '2588f6b42f9499527a34cda0574e0517f138745d3659e73041b21055a3e923f4'
            'cee214ebd9b3d905fb502a8a5009015198774a1c18afd49a870ce57e14ddd00d'
            '303fbd53d7502582f36acd384f6b306607ffcd35aab4a94b057307734ece51ae'
            '2391b6d4b9fc3c9c5da9ef61187af0dc5f17d2277ab1e0d3e9a7d4a0dbf6338c'
            'd2891e814ac6e52f73bab26256234631a900eb93ff28f942e6cc06ca1a8d9c91')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  tar -xf graphviz-${pkgver}.tar.gz || true

  cd "${srcdir}"/${_realname}-${pkgver}

  _apply_patch_with_msg \
    0001-configure-check-freeglut.patch \
    0002-gvedit-fix-relative-qmake-dir.patch \
    0003-gdiplus-fix-library.patch \
    0004-gvpr-remove-buiddir.patch \
    0005-makefile-no-undefined.patch \
    0006-tclpkg-fix-dll-extension.patch \
    0007-graphviz-workaround-ghostscript-check.patch

  ./autogen.sh
}

build() {
  local gd_incdir=$(pkg-config --variable=includedir gdlib)

  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  # else the libgdi plugin refuses to link.
  export lt_cv_deplibs_check_method=${lt_cv_deplibs_check_method='pass_all'}
  # the -enable-stuff below is for old type linking to graphviz 
  # and yields some nasty surprises when swig is enabled. 
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-guile=no \
    --enable-go=no \
    --enable-perl=no \
    --enable-php=no \
    --enable-ocaml=no \
    --enable-tcl=no \
    --enable-ruby=no \
    --enable-python=no \
    --enable-lua=no \
    --enable-r=no \
    --enable-sharp=no \
    --enable-swig=yes \
    --enable-java=no \
    --with-webp=yes \
    --with-gdiplus=yes \
    --with-smyrna=no \
    --with-gtk=no \
    --with-gtkglext=no \
    --with-gdk=no \
    --with-qt \
    --with-platformsdkincludedir=${MINGW_PREFIX}/include \
    --with-platformsdklibdir=${MINGW_PREFIX}/lib \
    --with-gdincludedir=${gd_incdir} \
    --enable-shared \
    --disable-static

  make
}

package_graphviz() {
  cd ${srcdir}/build-${MSYSTEM}

  local _pythonpath
  _pythonpath=$(python -c "import sysconfig; print(sysconfig.get_path('platlib'))")
  make DESTDIR="${pkgdir}" PYTHON3_INSTALL_DIR="$(cygpath ${_pythonpath})" install

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_graphviz-docs() {
  pkgdesc+=" (documentation)"
  depends=()
  optdepends=()

  cd "${srcdir}"/build-${MSYSTEM}
  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
